package com.aacar.controller;

import com.aacar.dto.Header;
import com.aacar.dto.MemberOrderListDto;
import com.aacar.dto.SubmitOrderDto;
import com.aacar.entity.Order;
import com.aacar.interceptor.MemLoginKeyValid;
import com.aacar.service.MemberOrderService;
import com.aacar.utils.ResultInfoUtil;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RestController
@RequestMapping("/memberorder")
public class MemberOrderController extends ValidController{
	private Logger logger = LoggerFactory.getLogger(this.getClass());
    
	@Resource
	private MemberOrderService memberOrderService;

	@Autowired
	private  HttpServletRequest request;


	@RequestMapping(value = "/submit/0", method = RequestMethod.POST)
	@ResponseBody
	@MemLoginKeyValid
	public Object detail(@RequestBody SubmitOrderDto submitOrderDto) throws Exception{
		logger.info("request:/memberorder/submit/0" + JSONObject.toJSONString(submitOrderDto));
		Header mobileHead = super.getMobileHead(request);
		submitOrderDto.setUserId(Long.valueOf(mobileHead.getUserId()));
		submitOrderDto.setDeviceTokens(mobileHead.getDeviceTokens());

		return ResultInfoUtil.setSuccessInfo(memberOrderService.submitOrder(submitOrderDto),mobileHead.getService());
    }

	@RequestMapping(value = "/list/0", method = RequestMethod.POST)
	@ResponseBody
	@MemLoginKeyValid
	public Object list(@RequestBody MemberOrderListDto memberOrderListDto){
		logger.info("request:/memberorder/list/0" + JSONObject.toJSONString(memberOrderListDto));

		Header mobileHead = super.getMobileHead(request);
		memberOrderListDto.setUserId(Long.valueOf(mobileHead.getUserId()));

		return ResultInfoUtil.setSuccessInfo(memberOrderService.getMemberOrderListPage(memberOrderListDto),mobileHead.getService());
	}


	@RequestMapping(value = "/detail/0", method = RequestMethod.POST)
	@ResponseBody
	@MemLoginKeyValid
	public Object list(@RequestBody Order order){
		Header mobileHead = super.getMobileHead(request);
		logger.info("request:/memberorder/detail/0" + JSONObject.toJSONString(order));
		return ResultInfoUtil.setSuccessInfo(memberOrderService.getMemberOrderDetail(order.getOrderNo()),mobileHead.getService());
	}

	/**
	 * 会员取消订单
	 */
	@RequestMapping(value = "/cancel/0", method = RequestMethod.POST)
	@ResponseBody
	@MemLoginKeyValid
	public Object cancel(@RequestBody Order order){
		Header mobileHead = super.getMobileHead(request);
		order.setUserId(Long.valueOf(mobileHead.getUserId()));
		logger.info("request:/memberorder/cancel/0" + JSONObject.toJSONString(order));
		return ResultInfoUtil.setSuccessInfo(memberOrderService.cancelMemberOrder(order.getOrderNo(),order.getUserId(),mobileHead.getDeviceTokens()),mobileHead.getService());
	}



   
}
